from typing import *
from sortedcontainers import SortedSet


class Solution:
    def processQueries(
        self, c: int, connections: List[List[int]], queries: List[List[int]]
    ) -> List[int]:

        p = [i for i in range(c + 1)]

        def find(a):
            if p[a] != a:
                p[a] = find(p[a])
            return p[a]

        def union(a, b):
            p[find(b)] = find(a)

        for a, b in connections:
            union(a, b)

        h = DefaultDict(SortedSet)
        for i in range(1, c + 1):
            h[find(i)].add(i)

        online = [True] * (c + 1)

        ans = []

        def query(a):
            if online[a]:
                return a
            else:
                while h[find(a)]:
                    if online[h[find(a)][0]]:
                        return h[find(a)][0]
                    h[find(a)].pop(0)
                return -1

        for a, b in queries:
            if a == 1:
                ans.append(query(b))
            elif a == 2:
                online[b] = False
        return ans


s = Solution()
print(
    s.processQueries(
        c=5,
        connections=[[1, 2], [2, 3], [3, 4], [4, 5]],
        queries=[[1, 3], [2, 1], [1, 1], [2, 2], [1, 2]],
    )
)
print(s.processQueries(c=3, connections=[], queries=[[1, 1], [2, 1], [1, 1]]))
